package negocio.Vehiculo;
import integracion.Factoria.*;
import integracion.Vehiculo.*;
import integracion.Venta.DAOVenta;
import integracion.Modelo.*;
import integracion.Color.*;
import integracion.Motor.*;

public class SAVehiculoImp implements SAVehiculo {
	

	@Override
	public boolean crearVehiculo(TransferVehiculo t) {
		 FactoriaDAO FacTdao = FactoriaDAO.getInstancia();
		 DAOVehiculo daoVehiculo = FacTdao.newDAOVehiculo();
		 DAOModelo daoModelo = FacTdao.newDAOModelo();
		 DAOMotor daoMotor = FacTdao.newDAOMotor();
		 DAOColor daoColor = FacTdao.newDAOColor();
		 if(t.getPrecio()<=0) 
			 return false;
		 if(t.getStock()<0)
			 return false;
		 if(daoColor.searchable(t.getColor()) && daoMotor.searchable(t.getMotor()) && daoModelo.searchable(t.getModelo()))
		 if(daoVehiculo.creable(t))
			 return daoVehiculo.create(t);
		 return false;
	}

	@Override
	public boolean eliminarVehiculo(TransferVehiculo t) {
		
		FactoriaDAO FactDao = FactoriaDAO.getInstancia();
		DAOVehiculo daoVehiculo = FactDao.newDAOVehiculo();
		DAOVenta daoVentas = FactDao.newDAOVenta();		
		if(daoVehiculo.deletable(t) && !daoVentas.containsVehiculo(t.getId()))
			return daoVehiculo.delete(t);
		return false;
			
	}

	@Override
	public TransferVehiculo buscarVehiculo(int id) {
		
		FactoriaDAO FactDao = FactoriaDAO.getInstancia();
		DAOVehiculo daoVehiculo = FactDao.newDAOVehiculo();
		if(daoVehiculo.searchable(id))
			return daoVehiculo.readById(id);
		return null;
		}

	@Override
	public boolean actualizarVehiculo(TransferVehiculo t) { 		
		FactoriaDAO FactDao = FactoriaDAO.getInstancia();
		DAOVehiculo daoVehiculo = FactDao.newDAOVehiculo();
		DAOModelo daoModelo = FactDao.newDAOModelo();
		DAOMotor daoMotor = FactDao.newDAOMotor();
		DAOColor daoColor = FactDao.newDAOColor();
		if(daoColor.searchable(t.getColor()) && daoMotor.searchable(t.getMotor()) && daoModelo.searchable(t.getModelo()))
		if(daoVehiculo.updatable(t))
			return daoVehiculo.update(t);
		return false;				
	}	
}
	
